package com.kupal.ahk.event;

import com.kupal.ahk.dict.Key;

import java.awt.*;

/**
 * Represent operations with mouse
 * @author Krauchenia Sergey
 *         Date: 13.10.2010
 *         Time: 15:37:40
 */
public class Mouse extends Event {

    /**
     * Emulate left mouse click (press and release)
     * @param x X position
     * @param y Y position
     */
    public static void click(int x, int y) {
        click(x,y,false);
    }

    /**
     * Emulate left mouse click (press and release)
     * in relative coordinates
     * @param x X position
     * @param y Y position
     * @param relative if true - calculate coordinates
     * relative to current cursor position
     */
    public static void click(int x, int y, boolean relative) {
        move(x,y, relative);
        ROBOT.mousePress(Key.MOUSE_LEFT.id);
        ROBOT.mouseRelease(Key.MOUSE_LEFT.id);
    }

    /**
     * Drug item from specified start mouse (relative?)
     * position (x1,y1) to specified (relative?) coordinates (x2,y2)
     * and then drop it
     * @param x1 start X position
     * @param y1 start Y position
     * @param x2 end X position
     * @param y2 end Y position
     * @param relative if true - calculate coordinates
     * relative to current cursor position
     */
    public static void dnd(int x1, int y1, int x2, int y2, boolean relative) {
        move(x1,y1, relative);
        ROBOT.mousePress(Key.MOUSE_LEFT.id);
        move(x2,y2, relative);
        ROBOT.mouseRelease(Key.MOUSE_LEFT.id);
    }

    /**
     * Drug item from specified start mouse position (x1,y1) to
     * specified coordinates (x2,y2) and then drop
     * it
     * @param x1 start X position
     * @param y1 start Y position
     * @param x2 end X position
     * @param y2 end Y position
     */
    public static void dnd(int x1, int y1, int x2, int y2) {
        dnd(x1,y1,x2,y2,false);
    }

    /**
     * Drug item from current mouse position to
     * specified relative coordinates and then drop
     * it
     * @param x relative X position
     * @param y relative Y position
     */
    public static void dnd(int x, int y) {
        dnd(0,0,x,y,true);
    }

    /**
     * Emulate double left mouse click (press and release)
     * @param x X position
     * @param y Y position
     */
    public static void dblClick(int x, int y) {
        dblClick(x,y,false);
    }

    /**
     * Emulate left mouse click (press and release)
     * in relative coordinates
     * @param x X position
     * @param y Y position
     * @param relative if true - calculate coordinates
     * relative to current cursor position
     */
    public static void dblClick(int x, int y, boolean relative) {
        int currentAutoDelay = ROBOT.getAutoDelay();
        ROBOT.setAutoDelay(10);
        click(x,y,relative);
        click(x,y,relative);
        ROBOT.setAutoDelay(currentAutoDelay);
    }

    /**
     * Emulate right mouse click (press and release)
     * @param x X position
     * @param y Y position
     */
    public static void rightClick(int x, int y) {
        rightClick(x,y,false);
    }

    /**
     * Emulate right mouse click (press and release)
     * in relative coordinates
     * @param x X position
     * @param y Y position
     * @param relative if true - calculate coordinates
     * relative to current cursor position
     */
    public static void rightClick(int x, int y, boolean relative) {
        move(x,y, relative);
        ROBOT.mousePress(Key.MOUSE_RIGHT.id);
        ROBOT.mouseRelease(Key.MOUSE_RIGHT.id);
    }

    /**
     * Move mouse to absolute coordinates
     * @param x X position
     * @param y Y position
     */
    public static void move(int x, int y) {
        move(x, y, false);
    }

    /**
     * Move mouse to coordinates
     * @param x X position
     * @param y Y position
     * @param relative if true - calculate coordinates
     * relative to current cursor position
     */
    public static void move(int x, int y, boolean relative) {
        int _x = relative ? x() + x : x;
        int _y = relative ? y() + y : y;

        ROBOT.mouseMove(_x, _y);
    }

    /**
     * Moving mouse up related to its current coordinates
     * @param distance positive number of pixels to move
     */
    public static void moveUp(int distance) {
        move(0, -distance, true);
    }

    /**
     * Moving mouse down related to its current coordinates
     * @param distance positive number of pixels to move
     */
    public static void moveDown(int distance) {
        move(0, distance, true);
    }

    /**
     * Moving mouse to the left related to its current coordinates
     * @param distance positive number of pixels to move
     */
    public static void moveLeft(int distance) {
        move(-distance, 0, true);
    }

    /**
     * Moving mouse to the right related to its current coordinates
     * @param distance positive number of pixels to move
     */
    public static void moveRight(int distance) {
        move(distance, 0, true);
    }

    /**
     * Retrieving absolute X position of mouse
     * @return X coordinate
     */
    public static int x() {
        return (int)MouseInfo.getPointerInfo().getLocation().getX();
    }

    /**
     * Retrieving absolute Y position of mouse
     * @return Y coordinate
     */
    public static int y() {
        return (int)MouseInfo.getPointerInfo().getLocation().getY();
    }

    public static int pixelColor(int x, int y) {
        return pixelColor(x,y,false);
    }

    public static int pixelColor(int x, int y, boolean relative) {
        return color(x,y,relative).getRGB();
    }

    public static Color color(int x, int y, boolean relative) {
        int _x = relative ? x() + x : x;
        int _y = relative ? y() + y : y;

        return ROBOT.getPixelColor(_x,_y);
    }

    public static Color color(int x, int y) {
        return color(x,y,false);
    }
}
